﻿using Rainbow.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Rainbow.view
{
    /// <summary>
    /// ProgressingDlg.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressingDlg : Window
    {
        public ProgressDlgViewModel windowViewModel;
        private BackgroundWorker bgWorker = null;
        private DoWorkEventHandler workerHandler = null;

        private delegate void DelegateDoWork(object s, DoWorkEventArgs args);
        private delegate void DelegateCompleteWork(object s, RunWorkerCompletedEventArgs args);

        DelegateDoWork delegateDoWork;
        DelegateCompleteWork delegateCompleteWork;

        public ProgressingDlg()
        {
            InitializeComponent();

            this.KeyDown += new KeyEventHandler(Window_KeyDown);
            this.Loaded += new RoutedEventHandler(ProgressingDlg_Loaded);

            windowViewModel = new ProgressDlgViewModel();
            this.DataContext = windowViewModel;

        }

        public ProgressingDlg(string title, string prompt, DoWorkEventHandler workerHandler) : this()
        {
            windowViewModel.title = title;
            windowViewModel.prompt = prompt;
            this.workerHandler = workerHandler;
        }

        private void ProgressingDlg_Loaded(object sender, RoutedEventArgs e)
        {
            bgWorker = new BackgroundWorker();
            if(workerHandler  != null)
            {
                delegateDoWork += new DelegateDoWork((object s, DoWorkEventArgs args) =>
                {
                    Thread.Sleep(1000);
                    workerHandler(s, args);
                });
                delegateCompleteWork += new DelegateCompleteWork((object s, RunWorkerCompletedEventArgs args) =>
                {
                    this.Close();
                });
                bgWorker.DoWork += new DoWorkEventHandler((object s, DoWorkEventArgs args) =>
                {
                    delegateDoWork(s, args);
                });
            }

            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((object s, RunWorkerCompletedEventArgs args) =>
            {
                delegateCompleteWork(s, args);
            });

            if(!this.bgWorker.IsBusy)
            {
                this.bgWorker.RunWorkerAsync();
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {

        }
    }

    public class ProgressDlgViewModel : ViewModelBase
    {
        public ProgressDlgViewModel()
        {

        }

        private string m_title;
        public string title
        {
            get { return m_title; }
            set { 
                m_title = value;
                NotifyPropertyChanged();
            }
        }

        private string m_prompt;
        public string prompt
        {
            get { return m_prompt; }
            set
            {
                m_prompt = value;
                NotifyPropertyChanged();
            }
        }
    }
}
